<template>
    <view class="goods-evaluate-container">
        <z-paging 
            ref="paging" 
            class="evaluate-list"
            v-model="goodsEvaluateList"
            :scroll-with-animation="true"
            :default-page-size="params.page_size"
            @query="handlerPullRefreshAndMore"
            @scroll="handleScroll"
        >
            <!-- 头部区域开始 -->
            <template #top>
                <!-- 搜索区域开始 -->
                <TridentFreshSearch @search="searchEvaluate"/>
                <!-- 搜索区域结束 -->

                <!-- 过滤tab区域开始 -->
                <view class="filter-tab-wrap">
                    <view class="filter-tab-item" @click="selectFilterTabs(index)" v-for="(item, index) in tabList" :key="index">
                        <text class="filter-tab-txt" :class="{active: currentTabIndex == index}">{{ item.text }}</text>
                        <text class="icon iconfont icon-shangjiantou active" v-if="currentTabIndex == index"></text>
                        <text class="icon iconfont icon-xiajiantou1" v-else></text>
                    </view>
                </view>
                <!-- 过滤tab区域结束 -->


                <!-- 过滤内容区域开始 -->
                <view class="filter-wrap" v-if="currentTabIndex != -1">
                    <!-- 过滤 -->
                    <template v-if="currentTabIndex == 0">
                        <view class="filter-item" v-for="(item, index) in filterList" :key="index" @click="handlerChangeFilter(item.value)">
                            <text class="filter-txt" :class="{active: filterValue == item.value}">{{ item.text }}</text>
                            <text class="icon iconfont icon-duigou" :class="{active: filterValue == item.value}" v-if="filterValue == item.value"></text>
                        </view>
                    </template>

                    <!-- 排序 -->
                    <template v-else-if="currentTabIndex == 1">
                        <view class="filter-item" v-for="(item, index) in sortList" :key="index" @click="handlerChangeSort(item.value)">
                            <text class="filter-txt" :class="{active: sortValue == item.value}">{{ item.text }}</text>
                            <text class="icon iconfont icon-duigou" :class="{active: sortValue == item.value}" v-if="sortValue == item.value"></text>
                        </view>
                    </template>
                </view>
                <!-- 过滤内容区域结束 -->

            </template>

            <!-- 内容区域开始 -->
            <view class="evaluate-wrap">
                <!-- 骨架屏 -->
                <view class="evaluate-skeleton" v-if="isShowSkeleton" v-for="(item, index) in 3" :key="index">
                    <uv-skeletons :loading="isShowSkeleton" :skeleton="skeleton"></uv-skeletons>
                </view>

                <view class="evaluate-item" v-for="(item, index) in goodsEvaluateList" :key="index">
                    <!-- 商品信息区域开始 -->
                    <view class="title-wrap" @click="goto('/pagesGoods/goods_detail/index?id='+item.goods_id)">
                        <text class="goods-name">{{ item.goods_name }}</text>
                        <text class="icon iconfont icon-youjiantoucu"></text>
                    </view>
                    <!-- 商品信息区域结束 -->

                    <view class="goods-wrap">
                        <text>{{ item.goods_sku_text }}</text>
                    </view>

                    <!-- 评价内容区域 -->
                    <view class="content">
                        <text class="txt">{{ item.contents }}</text>
                        <view class="image-wrap">
                            <view class="image-item" v-for="(image_url, index) in item.image_url_items" :key="index">
                                <image
                                    class="img"
                                    :src="global.FILE_DOMAIN + image_url"
                                    mode="scaleToFill"
                                />
                            </view>
                        </view>
                    </view>

                    <!-- 商家回复区域 -->
                    <view class="reply-wrap">
                        <text class="title">商家回复</text>
                        <text class="content">感谢亲的支持!欢迎下次光临!</text>
                    </view>

                    <!-- 评分区域开始 -->
                    <view class="score-wrap">
                        <view class="score-item">
                            <text>综合评价</text>
                            <uni-rate :isFill="false" :readonly="true" activeColor="#008000" size="22" v-model="item.composite_score"/>
                        </view>

                        <view class="score-item">
                            <text>描述相符</text>
                            <uni-rate :readonly="true" size="18" v-model="item.conform_score"/>
                        </view>

                        <view class="score-item">
                            <text>物流服务</text>
                            <uni-rate :readonly="true" size="18" v-model="item.logistic_score"/>
                        </view>

                        <view class="score-item">
                            <text>服务态度</text>
                            <uni-rate :readonly="true" size="18" v-model="item.serve_score"/>
                        </view>
                    </view>
                    <!-- 评分区域结束 -->

                    <!-- 操作区域开始 -->
                    <view class="option-wrap">
                        <view class="option-item" @click="handlerDelete(item)">
                            <text class="txt">删除评论</text>
                        </view>
                    </view>

                </view>
            </view>
            <!-- 内容区域结束 -->

            <!-- 加载更多提示 -->
            <template #loadingMoreNoMore>
                <view :style=" safeAreaInsets.bottom > 0 ? 'padding-bottom: ' + safeAreaInsets.bottom + 'px': ''" class="no-more" v-if="pageTotal == params.page && pageTotal > 1">我也是有底线的！</view>
            </template>
    
        </z-paging>

        <!-- 返回顶部 -->
		<TridentFreshBackTop @backTop="backTop" :isShowBackTop="isShowBackTop"/>
    </view>
</template>


<script setup lang="ts">
import { ref, inject, reactive } from "vue"

import { useUserStore } from '@/stores'
import { reqGoodsEvaluateListAPI, reqGoodsEvaluateTotalAPI, reqDeleteGoodsEvaluateAPI } from '@/apis/goodsEvaluate'



// ------------------ 定义全局变量开始 ------------------
// 用户存储
const userStore = useUserStore()

// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()

// 请求参数
const params = reactive({
    uid: userStore.profile.user.uid,
    goods_name: '',
    is_append: 'all',
    label_id: 0,
    is_image: 'all',
    composite_score: 0,
    sort_field: 'id',
    sort_type: 'desc',
    page: 1,
    page_size: 10
})


// 定义商品评价列表
const goodsEvaluateList:any = ref([])

// 商品评价数量
const total = ref(0)

// 总页数
const pageTotal = ref(0)

// 全局属性
const global:any = inject('global')

const paging = ref()
// ------------------ 定义全局变量结束 ------------------



// ------------------ 定义骨架变量开始 ------------------
// 定义是否显示骨架
const isShowSkeleton = ref(false)

// 定义骨架参数
const skeleton = reactive([
    {
      type: 'flex',
      num: 1,
      style: 'padding:25rpx 25rpx 0 25rpx;',
      children: [
        {
          type: 'avatar',
          num: 1,
          style: 'marginRight: 15rpx;'
        },
        {
          type: 'line',
          num: 2,
          style: 'width: 40%'
        },
      ]
    },
    {
        type: 'flex',
        num: 1,
        style: 'padding:25rpx;',
        children: [
            {
                type: 'line',
                num: 2,
            }
        ]
    },
    // 图片区域
    {
        type: 'flex',
        num: 1,
        style: 'padding:0 25rpx;justify-content: space-evenly;',
        children: [
            {
                type: 'custom',
                num: 1,
                style: 'width: 150rpx;height:150rpx;'
            },
            {
                type: 'custom',
                num: 1,
                style: 'width: 150rpx;height:150rpx;'
            },
            {
                type: 'custom',
                num: 1,
                style: 'width: 150rpx;height:150rpx;'
            },
            {
                type: 'custom',
                num: 1,
                style: 'width: 150rpx;height:150rpx;'
            },
        ]
    },
    20,
    // 商家回复
    {
        type: 'flex',
        num: 1,
        style: 'padding:0 25rpx;',
        children: [
            {
                type: 'custom',
                num: 1,
                style: 'height:50rpx;width:200rpx;margin-right:20rpx',
            },
            {
                type: 'line',
                num: 1,
                style: 'width: 70%',
            }
        ]
    },
    20,
    // 操作
    {
        type: 'flex',
        num: 1,
        style: 'padding:0 25rpx;justify-content: space-between;',
        children: [
            {
                type: 'custom',
                num: 1,
                style: 'height:50rpx;width:200rpx;',
            },
            {
                type: 'custom',
                num: 1,
                style: 'height:50rpx;width:200rpx;',
            },
            {
                type: 'custom',
                num: 1,
                style: 'height:50rpx;width:200rpx;',
            },
        ]
    },
    20
])
// ------------------ 定义骨架变量结束 ------------------



// 清除商品列表参数
const clearQueryParams = () => {
    params.is_append = 'all'
    params.label_id = 0
    params.is_image = 'all'
    params.composite_score = 0

	// params.sort_field = 'id'
	// params.sort_mode = 'desc'
	params.page = 1
	goodsEvaluateList.value = []
}



// -------------------- 搜索评价功能开始 --------------------
const searchEvaluate = (e:any) => {
    clearQueryParams()
    params.goods_name = e.keyword.value
    paging.value.reload()
}
// -------------------- 搜索评价功能结束 --------------------



// ------------------ 商品评价选项功能开始 ------------------
// 当前 Tab 索引
const currentTabIndex = ref(-1)

// 定义选项数据
const tabList = ref([
    { text: '全部', value: 0 },
    { text: '默认排序', value: 1 }
])

// 选择 tab
const selectFilterTabs = async (index:any) => {
    paging.value.scrollToTop()
    currentTabIndex.value = index
    // 选中全部
    if (index == 0) {
        await Promise.all([
            // 获取有图片的评价数量
            getGoodsEvaluateTotal({uid: userStore.profile.user.uid, goods_name: params.goods_name, is_image: '1', mode: 'image'}),
            // 获取有追加的评价数量
            getGoodsEvaluateTotal({uid: userStore.profile.user.uid, goods_name: params.goods_name, is_append: '1', mode: 'append'}),
            // 获取好评数量
            getGoodsEvaluateTotal({uid: userStore.profile.user.uid, goods_name: params.goods_name, composite_score: 5, mode: 'praise'})
        ])
    }
}
// ------------------ 商品评价选项功能结束 ------------------



// ------------------ 商品评价过滤功能开始 ------------------
// 筛选值
const filterValue = ref(0)

// 过滤条件
const filterList = ref([
    { text: '全部', value: 0 },
    { text: '视频/图片(0)', value: 1 },
    { text: '追评(0)', value: 2 },
    { text: '好评(0)', value: 3 },
])

// 处理过滤
const handlerChangeFilter = (value:any) => {

    clearQueryParams()
    
    filterValue.value = value
    tabList.value[0].text = filterList.value[value].text

    if (value == 1) { // 视频/图片
        params.is_image = '1'
    } else if (value == 2) { // 追评
        params.is_append = '1'
    } else if (value == 3) { // 好评
        params.composite_score = 5
    }

	// 当tab切换或搜索时，可以通过paging.value.reload()刷新整个列表
    paging.value.reload()
    currentTabIndex.value = -1
}
// ------------------ 商品评价过滤功能结束 ------------------



// ------------------ 商品评价排序功能开始 ------------------
// 排序值
const sortValue = ref(0)

// 排序条件
const sortList = ref([
    { text: '默认排序', value: 0 },
    { text: '评分高到低', value: 1 },
    { text: '评分低到高', value: 2 },
])

// 处理排序
const handlerChangeSort = (value:any) => {
    sortValue.value = value
    tabList.value[1].text = sortList.value[value].text
    if (value == 1) { // 评分高到低
        params.sort_field = 'composite_score'
        params.sort_type = 'desc'
    } else if (value == 2) { // 评分低到高
        params.sort_field = 'composite_score'
        params.sort_type = 'asc'
    } else {
        params.sort_field = 'id'
	    params.sort_type = 'desc'
    }

    params.page = 1
	goodsEvaluateList.value = []
    // 当tab切换或搜索时，可以通过paging.value.reload()刷新整个列表
    paging.value.reload()
    currentTabIndex.value = -1
}
// ------------------ 商品评价排序功能结束 ------------------



// -------------------- 返回顶部功能开始 --------------------
// 定义是否显示返回顶部
const isShowBackTop = ref(false)

// 返回顶部
const backTop = () => {
    paging.value.scrollToTop()
}

// 当滚动触发时，调用事件用于显示、隐藏返回顶部图标
const handleScroll = (e:any) => {
    isShowBackTop.value = e.detail.scrollTop > 300 ? true : false
}
// -------------------- 返回顶部功能结束 --------------------



// -----------------下拉刷新 + 加载更多开始 -----------------
const handlerPullRefreshAndMore = (pageNo:any, pageSize:any) => {
    params.page = pageNo
    getGoodsEvaluateList()
}
// -----------------下拉刷新 + 加载更多结束 -----------------



// ------------------ 获取评价列表功能开始 ------------------
const getGoodsEvaluateList = async () => {
    // 显示骨架
    if (params.page == 1) {
        isShowSkeleton.value = true
    }

	// 请求数据
	const result:any = await reqGoodsEvaluateListAPI(params)
	let goodsComments = result.data.goods_comments != null ? result.data.goods_comments : []
    total.value = result.data.total
    pageTotal.value = result.data.page_total
    paging.value.complete(goodsComments);

    // 隐藏骨架
    isShowSkeleton.value = false
}

// 获取评价数量
const getGoodsEvaluateTotal = async (queryParam:any) => {
    // 请求数据
	const result:any = await reqGoodsEvaluateTotalAPI(queryParam)
    if (queryParam.mode == 'image') {
        filterList.value[1].text = '视频/图片('+ result.data.total +')'
    } else if (queryParam.mode == 'append') {
        filterList.value[2].text = '追评('+ result.data.total +')'
    } else if (queryParam.mode == 'praise') {
        filterList.value[3].text = '好评('+ result.data.total +')'
    }
}
// ------------------ 获取评价列表功能结束 ------------------



// ---------------------- 删除功能开始 ----------------------
const handlerDelete = async (item:any) => {
    const id = item.id
    uni.showModal({
        content: '确定要删除吗?',
        success: async (res) => {
	        if (res.confirm) {
                await reqDeleteGoodsEvaluateAPI(id)
                uni.showToast({
                    title: '删除成功',
                    duration: 1000
                })

                const index = goodsEvaluateList.value.findIndex((item:any) => item.id == id)
                goodsEvaluateList.value.splice(index, 1)
	        }
	    }
    })
}
// ---------------------- 删除功能结束 ----------------------

const goto = (url:any) => {
    uni.navigateTo({url})
}
</script>

<style scoped lang="scss">
.goods-evaluate-container {
    .evaluate-list {
        // 过滤区域
        .filter-tab-wrap {
            height: 120rpx;
            line-height: 120rpx;
            display: flex;
            justify-content: space-evenly;
            background-color: #fff;
            border-bottom: 2rpx solid #ccc;
            .filter-tab-item {
                .filter-tab-txt {
                    font-size: 32rpx;
                }
                .icon {
                    font-size: 28rpx;
                    color: #ccc;
                    padding-left: 10rpx;
                }
                .active {
                    color: #008000;
                }
            }
        }

        .filter-wrap {
            background-color: rgba(0, 0, 0, 0.7);
            height: 100vh;
            .filter-item {
                background-color: #fff;
                height: 80rpx;
                line-height: 80rpx;
                padding: 0 20rpx;

                display: flex;
                justify-content: space-between;
                border-bottom: 2rpx solid #ccc;
                &:last-child {
                    border-bottom: none;
                }
                .icon {
                    font-size: 40rpx;
                }
            }
            .composite-filter-wrap {
                background-color: #fff;
                .scroll-view {
                    height: calc(100vh - 500rpx);
                    .sku-wrap {
                        .sku-list {
                            .sku-item {
                                .sku-title {
                                    font-size: 32rpx;
                                    text-align: center;
                                    padding:20rpx 0;
                                    color: #008000;
                                    border-bottom: 2rpx dashed #ccc;
                                    font-weight: bold;
                                }
                                .sku-content {
                                    display: flex;
                                    flex-wrap: wrap;
                                    padding-top: 10rpx;
                                    .sku-txt {
                                        border: 2rpx solid #ccc;
                                        padding: 4rpx;
                                        margin: 14rpx  6rpx;
                                        border-radius: 2rpx;
                                        width: 46%;
                                    }
                                    .active {
                                        color: #fff;
                                        background-color: #008000;
                                    }
                                }
                            }
                        }
                    }
                }

                .btn-wrap {
                    height: 100rpx;
                    line-height: 100rpx;
                    margin-top: 20rpx;
                    text-align: center;
                    border-top: 2rpx solid #ccc;
                    .btn {
                        padding: 10rpx 80rpx;
                        border-radius: 4rpx;
                    }
                    .reset {
                        margin-right: 40rpx;
                        border: 2rpx solid #ccc;
                        color: #ccc;
                    }
                    .confirm {
                        background-color: #008000;
                        color: #fff;
                    }
                }
            }
            .active {
                color: #008000;
            }
        }

        .evaluate-wrap {
            padding-bottom: 20rpx;
            .evaluate-skeleton {
                background-color: #fff;
                margin: 10rpx;
                border-radius: 8rpx;
            }
            .evaluate-item {
                background-color: #fff;
                margin: 0 10rpx;
                border-radius: 10rpx;
                padding: 20rpx;
                margin-top: 15rpx;
                .title-wrap {
                    font-size: 28rpx;
                    border-bottom: 2rpx solid #ccc;
                    padding-bottom: 10rpx;

                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    .goods-name {
                        font-weight: bold;
                    }
                }

                // 商品信息区域
                .goods-wrap {
                    padding: 20rpx 0;
                    color: #ccc;
                    font-size: 26rpx;
                }

                .content {
                    font-size: 26rpx;
                    // 评价图片
                    .image-wrap {
                        margin: 20rpx 0;
                        display: flex;
                        .image-item {
                            width: 25%;
                            height: 160rpx;
                            padding-right: 10rpx;
                            &:nth-child(4n) {
                                padding-right: 0;
                            }
                            .img {
                                width: 100%;
                                height: 100%;
                            }
                        }
                    }
                }

                // 商家回复
                .reply-wrap {

                    .title {
                        font-size: 30rpx;
                        font-weight: bold;
                        padding-right: 10rpx;
                    }
                }
                
                // 评分区域
                .score-wrap {
                    padding-top: 20rpx;
                    .score-item {
                        display: flex;
                        align-items: center;

                        padding-bottom: 14rpx;

                        &:last-child {
                            padding-bottom: 0;
                        }
                    }
                }

                // 操作区域
                .option-wrap {
                    margin-top: 20rpx;
                    display: flex;
                    justify-content: flex-end;
                    border-top: 2rpx solid #ccc;
                    padding-top: 20rpx;
                    .option-item {
                        .txt {
                            border: 1rpx solid #CF4444;
                            font-size: 28rpx;
                            padding: 4rpx 12rpx;
                            color: #CF4444;
                            border-radius: 4rpx;
                        }
                    }
                }
            }
        }

        // 没有更多区域
        .no-more {
            text-align: center;
            padding-bottom: 20rpx;
            color: #ccc;
            &::before {
                content: '-------------';
                margin-right: 10rpx;
            }
            &::after {
                content: '-------------';
                margin-right: 10rpx;
            }
        }
    }
}
</style>